package com.wazzapps.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GoogleApiAvailability;

import java.util.UUID;

public class WazzUserInfo {
    private boolean adLimited = false;
    private Context context = null;
    private String gaid = null;
    private boolean isPersonalizedEnabled = true;
    private boolean userIdPending = false;
    private String uuid = null;

    public WazzUserInfo(Context context, boolean z) {
        this.isPersonalizedEnabled = z;
        this.context = context;
        getGaidId();
    }

    public String getUserId() {
        if (this.uuid == null) {
            String savedToPrefsId = getSavedToPrefsId();
            this.uuid = savedToPrefsId;
            if (savedToPrefsId == null) {
                String uuid = UUID.randomUUID().toString();
                this.uuid = uuid;
                saveUserIdToPrefs(uuid);
            }
        }
        return this.uuid;
    }

    public boolean isAdLimited() {
        return this.adLimited || !this.isPersonalizedEnabled;
    }

    public String getUserAdvertisingId() {
        return this.gaid;
    }

    private String getSavedToPrefsId() {
        return this.context.getSharedPreferences(WazzApplicationInstance.USER_PREFS, 0).getString(WazzApplicationInstance.USER_KEY, null);
    }

    private void saveUserIdToPrefs(String str) {
        SharedPreferences.Editor edit = this.context.getSharedPreferences(WazzApplicationInstance.USER_PREFS, 0).edit();
        edit.putString(WazzApplicationInstance.USER_KEY, str);
        edit.apply();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setAdLimitedId(boolean z) {
        this.adLimited = z || !this.isPersonalizedEnabled;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setGaid(String str) {
        this.gaid = str;
    }

    private void getGaidId() {
        if (!this.userIdPending) {
            final Context applicationContext = this.context.getApplicationContext();
            if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(applicationContext) == 0) {
                AsyncTask r1 = new AsyncTask<Void, Void, String>() { // from class: com.wazzapps.sdk.WazzUserInfo.1
                    /* access modifiers changed from: protected */
                    public String doInBackground(Void... voidArr) {
                        try {
                            AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(applicationContext);
                            WazzUserInfo.this.setAdLimitedId(advertisingIdInfo.isLimitAdTrackingEnabled());
                            return advertisingIdInfo.getId();
                        } catch (Exception e) {
                            e.printStackTrace();
                            WazzUserInfo.this.setAdLimitedId(true);
                            return null;
                        }
                    }

                    /* access modifiers changed from: protected */
                    public void onPostExecute(String str) {
                        WazzUserInfo.this.setGaid(str);
                    }
                };
                this.userIdPending = true;
                r1.execute(new Void[0]);
                return;
            }
            setGaid(getUserId());
        }
    }

    public String getInfo() {
        return "User Id: " + getUserId() + "\nAdv id: " + getUserAdvertisingId() + "\nLimit Adv id: " + isAdLimited();
    }
}